package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
import ModelLayer.*;
 

/**
 * User interface class to the AddressBook
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class SupplierUI
{   

    private SupplierCtr supCtr;//reference to the ControlLayer
    
    //constructor which does nothing
    public SupplierUI()
    {
       supCtr = new SupplierCtr();
    }
    
    public void start()
    {
        supplierMenu();
    }
    
    public void supplierMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choise = writeSupplierMenu(); 
          if(choise == 1)
          {   //show person
              findSupplier();
          }
          else{
              if(choise == 2)
              {
                  createSupplier();
              }  
              else{
                  if(choise == 3)
                  {
                      deleteSupplier();
                  }  
                  else{
                      if(choise == 4)
                      {
                          updateSupplier();
                      }
                      
                          else{
                              exit = true;
                           }//end else
                        //end else
                    }//end else
                }//end else
            }//end else
            
       }//end while
    }
    public int writeSupplierMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Supplier Menu ***");
            System.out.println(" (1) Show information about a supplier");
            System.out.println(" (2) Add supplier");
            System.out.println(" (3) Delete supplier");
            System.out.println(" (4) Update supplier");
            System.out.println(" (5) Return");
            System.out.print("\n Make your choice: ");
            
            int choise = keyboard.nextInt();
            return choise;
    }
    
    public String inputCompanyName()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Company Name:  ");
         String companyName = keyboard.nextLine();
         return companyName;
    }
    
    public String inputCity()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write City:  ");
         String city = keyboard.nextLine();
         return city;
    }
    
    public String inputAddress()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Address:  ");
         String address = keyboard.nextLine();
         return address;
    }
    
    public int inputPostalCode()
    {
        // makes an object keyboard to have input from the screen
        boolean ok = false;
        int postalcode = 0;
        while(!ok)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Write postalcode:");
            try
            {
                postalcode = keyboard.nextInt();
                ok = true;
            }
            catch(Exception e)
            {
                System.out.println("input must be number - try again");
                String input = keyboard.nextLine();
            }
        }
        return postalcode;
    }

    
    public int inputEaNr()
    {
         // makes an object keyboard to have input from the screen
        boolean ok = false;
        int eanr = 0;
        while(!ok)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Write supplier eanr:");
            try
            {
                eanr = keyboard.nextInt();
                ok = true;
            }
            catch(Exception e)
            {
                System.out.println("input must be number - try again");
                String input = keyboard.nextLine();
            }
        }
        return eanr;
    }

    
    public void createSupplier()
    {
        String companyName = inputCompanyName();
        String city = inputCity();
        String address = inputAddress();
        int postalCode = inputPostalCode();
        int eanr = inputEaNr();
        supCtr.createSupplier(companyName, city, address, postalCode, eanr);
    }
    
    public void findSupplier()
    {
        boolean ok = false;
        while(!ok)
        {
            try
            {
                int eanr = inputEaNr();
                Supplier supplier = supCtr.findSupplier(eanr);
                System.out.println("Company Name: " + supplier.getCompanyName());
                System.out.println("City: " + supplier.getCity());
                System.out.println("Address: " + supplier.getAddress());
                System.out.println("Postalcode: " + supplier.getPostalCode());
                System.out.println("EA Number: " + supplier.getEaNr());
                ok = true;
                pause();
            }
            catch(Exception e)
            {
                System.out.println("The EA number doesn't exist - try again");
            }
        }
    }
    
    public void updateSupplier()
    {
        String companyName = inputCompanyName();
        String city = inputCity();
        String address = inputAddress();
        int postalCode = inputPostalCode();
        int aenr = inputEaNr();
        supCtr.updateSupplier(companyName, city, address, postalCode, aenr);
    }
    
    public void deleteSupplier()
    {
        boolean ok = false;
        while(!ok)
        {
            try
            {
                int eanr = inputEaNr();
                supCtr.deleteSupplier(eanr);
            }
            catch(Exception e)
            {
                System.out.println("The EA number doesn't exist - try again");
            }
        }
    }
    
    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }
}

    
